package fc.testmain;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import fc.FcHttpCon;
import fc.entity.OaEntity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class T100CUST_CREATE11 {


    public static void main(String[] args) {


        OaEntity oa=new OaEntity();
        oa.setJIANCHENG("FUNAME-测试简称11");
        oa.setQUANCHENG("FUNAME-测试全称11");
        oa.setCUR_ID("CNY");
        oa.setTAX_NO("T08");
        oa.setFKTJ("A12");

        oa.setUSERID("ZH00001344");
        oa.setDEPARTMENTID("10000193");
        oa.setZKLB("AP01");
        oa.setSHUIHAO("税号-测试001");
        oa.setCUS_NO("TEST00004");
        oa.setGJDQ("CN");

        List<OaEntity> dt1=new ArrayList<OaEntity>();
        OaEntity oa1=new OaEntity();
        oa1.setJYBH("001");oa1.setGJDQ("CN");oa1.setSFZY("Y");oa1.setDIZHI("地址1");
        dt1.add(oa1);
        OaEntity oa2=new OaEntity();
        oa2.setJYBH("002");oa2.setGJDQ("AB");oa2.setSFZY("N");oa2.setDIZHI("地址2");
        dt1.add(oa2);
        oa.setDT1(dt1);

        List<OaEntity> dt2=new ArrayList<OaEntity>();
        OaEntity oa3=new OaEntity();
        oa3.setTXLX("1");oa3.setTXNR("手机号");oa3.setSFZY("Y");
        dt2.add(oa3);
        OaEntity oa4=new OaEntity();
        oa4.setTXLX("4");oa4.setTXNR("邮箱");oa4.setSFZY("N");
        dt2.add(oa4);
        oa.setDT2(dt2);

        List<OaEntity> dt3=new ArrayList<OaEntity>();
        OaEntity oa5=new OaEntity();
        oa5.setYHBH("bianhao001");oa5.setYHZH("zhanghu001");oa5.setSFZY1("Y");oa5.setSFZY2("Y");
        dt3.add(oa5);
        OaEntity oa6=new OaEntity();
        oa6.setYHBH("bianhao002");oa6.setYHZH("zhanghu002");oa6.setSFZY1("N");oa6.setSFZY2("N");
        dt3.add(oa6);
        oa.setDT3(dt3);

        List<OaEntity> dt4=new ArrayList<OaEntity>();
        OaEntity oa7=new OaEntity();
        oa7.setJYTJ("A22");oa7.setSFZY("Y");

        dt4.add(oa7);
        OaEntity oa8=new OaEntity();
        oa8.setJYTJ("A26");oa8.setSFZY("N");
        dt4.add(oa8);
        oa.setDT4(dt4);


        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String  timestamp= format.format(new Date());
        DateFormat formataa = new SimpleDateFormat("yyyy-MM-dd");
        String createdate = formataa.format(new Date());


        String testparam="";


    String json_param=
    "{"+
    "\"key\": \"a5e7f741701e726aefeaaa58199a724f\","+
    "\"type\": \"sync\","+
    "\"host\": {"+
    "\"prod\": \"OAPI\","+
    "\"ip\": \"10.10.10.1\","+
    "\"lang\": \"zh_CN\","+
    "\"timestamp\": \""+timestamp+"\","+
    "\"acct\": \"tiptop\""+
    "},"+
    "\"service\": {"+
    "\"prod\": \"T100\","+
    "\"name\": \"supplier_crt\","+
    "\"ip\": \"10.10.10.75\","+
    "\"id\": \"topprod\""+
    "},"+
    "\"datakey\": {"+
    "\"EntId\": \"23\","+   //--2代表正式区， 23代表测试区
    "\"CompanyId\": \"ZH00\","+
    "\"supplier_no\": \""+oa.getCUS_NO()+"\""+  //--供应商完整编码
    "},"+
    "\"payload\": {"+
    "\"std_data\": {"+
    "\"parameter\": {"+
    "\"enterprise_no\": \"23\", "+  //--正式区，测试区这里填23
    "\"site_no\": \"ZH00\","+     //--据点，ZH00代表珠海迈为
    "\"is_digiwin_product\": \"Y\","+
    "\"supplier_detail\": ["+
    "{"+
    "\"supplier_no\": \""+oa.getCUS_NO()+"\","+   //--供应商完整编码，不要重复
    "\"supplier_full_name\": \""+oa.getQUANCHENG()+"\","+   //--不要重复
    "\"supplier_shortname\": \""+oa.getJIANCHENG()+"\","+  //--不要重复
    "\"trans_object_type\": \"1\","+
    "\"legal_type\": \"1\","+
    "\"belong_legal\": \""+oa.getCUS_NO()+"\","+   //--完整供应商编码
    "\"belong_group_no\": \"\","+
    "\"uniform_number\": \""+oa.getSHUIHAO()+"\","+    //--税号，不可重复
    "\"country_no\": \""+oa.getGJDQ()+"\","+ // 国家地区
    "\"industry_id\": \"\","+
    "\"monthly_settlement_day\": \"1\","+
    "\"payment_day\": \"1\","+
    "\"receivable_day\": \"1\","+
    "\"purchaser_no\": \""+oa.getUSERID()+"\","+   //--采购员编号
    "\"responsibility_department_no\": \""+oa.getDEPARTMENTID()+"\","+     //--归属部门编号
    "\"create_employee_no\": \""+oa.getUSERID()+"\","+   //--建档人的编号
    "\"currency_no\": \""+oa.getCUR_ID()+"\","+   //--币种
    "\"tax_no\": \""+oa.getTAX_NO()+"\", "+  //--税种
    "\"payment_condition_no\": \""+oa.getFKTJ()+"\","+   //--付款条件
    "\"trade_condition_no\": \""+oa.getJYTJ()+"\","+     //--交易条件
    "\"account_type\": \""+oa.getZKLB()+"\","+  //--帐款类别
    "\"create_date\": \""+createdate+"\","+
    "\"trans_object_relation_detail\": ["+
    "{"+
    "\"trade_type\": \"1\","+  //--1代表收付款对象类型
    "\"is_main\": \"Y\","+
    "\"trans_object_no\": \""+oa.getCUS_NO()+"\""+   //--完整供应商编号
    "}"+
    "],"+
    "\"contact_address_detail\": [";

    testparam="";
    for(int i=0;i<oa.getDT1().size();i++){

        if (!"".equals(testparam)){
            testparam=testparam+",";
        }
        testparam+=
        //地址的
        "{"+
                "\"brief_no\": \""+oa.getDT1().get(i).getJYBH()+"\","+
                "\"address_type\": \"1\","+   //--1代表登记地址
                "\"is_main_contact_address\": \""+oa.getDT1().get(i).getSFZY()+"\","+
                "\"country_code\": \""+oa.getDT1().get(i).getGJDQ()+"\","+
                "\"address\": \""+oa.getDT1().get(i).getDIZHI()+"\","+  //--地址信息
                "\"expiration_date\": \"9999-01-01\""+
        "}";
        //地址的
    }
    json_param+=testparam;


     json_param+=
    "],"+
    "\"communication_detail\": [";



    testparam="";
    for(int i=0;i<oa.getDT2().size();i++){

        if (!"".equals(testparam)){
            testparam=testparam+",";
        }
        testparam+=
        //联系方式
        "{"+
                "\"communication_type\": \""+oa.getDT2().get(i).getTXLX()+"\","+
                "\"communication_content\": \""+oa.getDT2().get(i).getTXNR()+"\","+  //--电话号码
                "\"is_main_communication_method\": \""+oa.getDT2().get(i).getSFZY()+"\","+
                "\"expiration_date\": \"9999-01-01\""+
         "}";
        //联系方式
    }
    json_param+=testparam;



     json_param+=
     "],"+
    "\"bank_info\": [";

    testparam="";
    for(int i=0;i<oa.getDT3().size();i++){

        if (!"".equals(testparam)){
            testparam=testparam+",";
        }
        testparam+=
         //银行信息
        "{"+
                "\"bank_code\": \""+oa.getDT3().get(i).getYHBH()+"\","+  //--银行编号
                "\"bank_account\": \""+oa.getDT3().get(i).getYHZH()+"\","+  //--银行帐号
                "\"main_receive_bank_account\": \""+oa.getDT3().get(i).getSFZY1()+"\","+
                "\"main_payment_bank_account\": \""+oa.getDT3().get(i).getSFZY2()+"\""+
         "}";
        //银行信息
    }
    json_param+=testparam;

    json_param+=
    "],"+
    "\"allow_payment_condition\": [";



    testparam="";
    for(int i=0;i<oa.getDT4().size();i++){

        if (!"".equals(testparam)){
            testparam=testparam+",";
        }
        testparam+=
        //付款条件
        "{"+
                "\"trade_condition_no\": \""+oa.getDT4().get(i).getFKTJ()+"\", "+  //--充许付款条件编号
                "\"is_main\": \""+oa.getDT4().get(i).getSFZY()+"\""+
         "}";
        //付款条件
    }
    json_param+=testparam;




     json_param+=
    "]"+
    "}"+
    "]"+
    "}"+
    "}"+
    "}"+
    "}";




        System.out.println(json_param);
        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(FcHttpCon.ZH_YFSY_XQ).body(json_param).execute().body();
        System.out.println(data);
        String result="ERROR";
        System.out.println("----------------");

        try {

            Object succesResponse = JSON.parse(data);
            Map map = (Map)succesResponse;
            Map map1 = (Map)map.get("payload");
            Map map2 = (Map)map1.get("std_data");
            Map map3 = (Map)map2.get("execution");
            if("0".equals(map3.get("code"))){
                result="SUCCESS";
            }else{
                Map map4 = (Map)map2.get("parameter");

                System.out.println(map4);

                JSONArray jsonArray = new JSONArray(map4.get("supplier_no"));
                System.out.println(jsonArray);
                for( int i=0;i<jsonArray.size();i++){
                    JSONObject infog = new JSONObject((Map<String, Object>) jsonArray.get(i));
                    Map mapg = (Map) infog;
                    result= mapg.get("execute_description").toString();//1
                }

            }

           // System.out.println(map1);
        } catch (Exception e) {
            System.out.println("T100CUST_CREATE接口报错了！！！！！！！！");
        }
        System.out.println("result:"+result);


    }
}
